package com.kws.tool.controller;
 
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
 
import java.io.*;
 
public class PDFToWord {
 
    public static void main(String[] args) {
        File inputWord = new File("C:\\Users\\Waytale\\Desktop\\接入时发现的问题.docx");
        File outputFile = new File("C:\\Users\\Waytale\\Desktop\\test.pdf");
//        try  {
//            InputStream docxInputStream = new FileInputStream(inputWord);
//            OutputStream outputStream = new FileOutputStream(outputFile);
//            IConverter converter = LocalConverter.builder().build();
//            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
//            outputStream.close();
//            System.out.println("success");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            convertWordToPdf(docxInputStream, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 将Word文件转换为PDF文件
     * @param docxInputStream Word文件输入流
     * @param outputStream PDF文件输出流
     */
    public static void convertWordToPdf(InputStream docxInputStream, OutputStream outputStream) {
        try {
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}